package com.share.finance.asset.sjd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.finance.asset.sjd.mapper.UserFileMapper;
import com.share.finance.asset.sjd.model.db.UserFile;
import com.share.finance.asset.sjd.service.IUserFileService;
import com.share.finance.asset.sjd.service.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户文件表  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-08
 */
@Service
public class UserFileServiceImpl extends ServiceImpl<UserFileMapper, UserFile> implements IUserFileService {
    @Resource
    private UserFileMapper userFileMapper;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public List<UserFile> listByUserId(String userId, String bizCode) {
        List<UserFile> reList = null;

        List<String> list = new LinkedList<>();
        list.add(userId);
        Map<String, List<UserFile>> tmpMap = listByUserIds(list, bizCode);
        if (!CollectionUtils.isEmpty(tmpMap)) {
            reList = tmpMap.get(userId);
        }
        return reList;
    }

    @Override
    public Map<String, List<UserFile>> listByUserIds(List<String> userIdList, String bizCode) {
        LambdaQueryWrapper<UserFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserFile::getUserId, userIdList);
        if (StringUtils.isNotBlank(bizCode)) {
            queryWrapper.eq(UserFile::getBusinessCode, bizCode);
        }
        queryWrapper.eq(UserFile::getIsDelete, "0");

        List<UserFile> list = userFileMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            Map<String, List<UserFile>> reMap = list.stream().filter(f -> StringUtils.isNotBlank(f.getUserId())).collect(Collectors.groupingBy(e -> e.getUserId()));
            return reMap;
        }
    }
}
